package com.jic.market.impl.allocation;


import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.market.common.enums.CostAllocationFlagEnum;
import com.jic.market.entity.AllocationModel;
import com.jic.market.request.after.TellAddAllocationRequest;
import com.jic.market.response.PatchGrouponThemeResponse;
import com.jic.market.service.PatchGrouponThemeService;
import com.jic.market.service.allocation.PromotionAddAllocationStrategy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xuyongliang
 * @version V1.0
 * @className PromotionAddAllocationStrategyGroupon
 * @description 拼团分摊策略
 * @date 2020/3/16
 */
@Slf4j
@Service("allocationGroupon")
public class PromotionAddAllocationStrategyGroupon implements PromotionAddAllocationStrategy {

    @Autowired
    PatchGrouponThemeService patchGrouponThemeService;

    @Override
    public AllocationModel collectionAllocation(TellAddAllocationRequest request) {
        log.info("--------进入拼团分摊策略tellRequest--{}-----",request);
        // 转成 long 类型
        Long promotionId =Long.valueOf(request.getPromotionId());
        PatchGrouponThemeResponse promotion = patchGrouponThemeService.getById(promotionId);

        // NO CostAllocationFlagEnum.SHARE.getCode().equals(promotion.getCostAllocationFlag())
        AllocationModel model =new AllocationModel();
        BeanUtils.copyProperties(promotion, model);
        if (promotion.getCostAllocationFlag().equals("1")){
            model.setParentAllocationScale(promotion.getParentAllocationScale());
            model.setChildAllocationScale(promotion.getChildAllocationScale());
            model.setMerchantAllocationScale(promotion.getMerchantAllocationScale());
            model.setStoreAllocationScale(promotion.getStoreAllocationScale());
        }else {
            //不分摊则判断当前活动组织等级设置默认值100
            if (promotion.getDeptType() == DeptTypeEnums.OPERATION_PLATFORM.getCode()){
                model.setParentAllocationScale(100);
            }else if (promotion.getDeptType() == DeptTypeEnums.CHILD_PLATFORM.getCode()){
                model.setChildAllocationScale(100);
            }else if (promotion.getDeptType() == DeptTypeEnums.MERCHANT_PLATFORM.getCode()){
                model.setMerchantAllocationScale(100);
            }else {
                model.setStoreAllocationScale(100);
            }
        }


        model.setPromotionId(promotion.getGroupCode());
        model.setCostAllocationFlag(promotion.getCostAllocationFlag());
        model.setDeptId(promotion.getDeptId());
        model.setDeptType(promotion.getDeptType());
        model.setPromType(promotion.getPromType());
        return model;
    }
}
